package Perlescent::Core;

use Exporter qw(import);

our(@ISA,@EXPORT,@EXPORT_OK);
@EXPORT = qw(
);
@EXPORT_OK = qw(
);

@ISA = qw( Perlescent );

use strict;
use warnings;
use Digest::MD5 qw(md5_hex);

my $utils = Perlescent->become('Utils');
my $tmpl = Perlescent->become('Theme');

sub new
{
    my $class = shift;
    my $self = {};
    bless $self, $class;
    my $caller = caller;
    $utils->writeLog("Core","INFO","Creating new object for $caller");
    return $self;
}

sub GetRecentPosts
{
	my $posts;

	my(@pids) = $utils->getGenericArray( {
	table => 'posts', field => 'id', status => 'publish', _type => 'post', order_by => '_date DESC', limit => 5
	} );

	for(my$i=0;$i<@pids;$i++)
	{
		$posts .= &GetPost($pids[$i]) . "\n";
	}

	return $posts;
}

sub GetPost
{
	shift if ref($_[0]);
	my$id = shift;
	my@pd = $utils->getGenericArray({ table => 'posts', field => '*', id => $id, status => 'publish', _type => 'post' });
		my$author = $pd[1];
	my$nick = $utils->getGeneric({ table => 'users', field => 'nick', id => $author });
		my$dt = $pd[2];
		my($date,$time) = (split /\s/, $dt);
		my$content = $pd[4];
		my$title = $pd[5];
		my$xcrpt = $pd[6];
		my$ctg = $pd[-5];
	my$cat = $utils->getGeneric({ table => 'cats', field => 'name', id => $ctg });
		my$c_cnt  = $pd[-1];

		my $post = "<div id='post-$id' class='post-$id post type-post hentry category-uncategorized'>
		<h2 class='entry-title'><a href='?p=$id' title='Permalink to $title' rel='bookmark'>". $pd[5] ."</a></h2>

			<div class='entry-meta'>
				<span class='meta-prep meta-prep-author'>Posted on </span>
				<a href='?p=1' title='$time' rel='bookmark'>
					<span class='entry-date'>$date</span></a>
					<span class='meta-sep'> by </span>
					<span class='author vcard'>
					<a class='url fn n' href='?author=$author' title='View all posts for $nick'>$nick</a>
					</span>
			</div><!-- .entry-meta -->

				<div class='entry-content'>
				<p>$content</p>
				</div><!-- .entry-content -->
	
			<div class='entry-utility'>
				<span class='cat-links'>
					<span class='entry-utility-prep entry-utility-prep-cat-links'>Posted in
					<a href='?cat=$ctg' title='View all posts in $cat' rel='category'>$cat</a>
					</span>
				</span>
				<span class='meta-sep'> | </span>

				<span class='comments-link'>
		<a href='?p=1#comments' title='Comment on $title'>$c_cnt Comments</a>
				</span>
							</div><!-- #entry-utility -->
		</div>";

	return $post;
}

sub GetPostWithComments
{
	shift if ref($_[0]);
	my$id = shift;
	my@pd = $utils->getGenericArray({ table => 'posts', field => '*', id => $id, status => 'publish', _type => 'post' });
		my$author = $pd[1];
	my$nick = $utils->getGeneric({ table => 'users', field => 'nick', id => $author });
		my$dt = $pd[2];
		my($date,$time) = (split /\s/, $dt);
		my$content = $pd[4];
		my$title = $pd[5];
		my$xcrpt = $pd[6];
		my$ctg = $pd[-5];
	my$cat = $utils->getGeneric({ table => 'cats', field => 'name', id => $ctg });
		my$c_cnt  = $pd[-1];

		my $post = "<div id='post-$id' class='post-$id post type-post hentry category-uncategorized'>
		<h2 class='entry-title'><a href='?p=$id' title='Permalink to $title' rel='bookmark'>". $pd[5] ."</a></h2>

			<div class='entry-meta'>
				<span class='meta-prep meta-prep-author'>Posted on </span>
				<a href='?p=1' title='$time' rel='bookmark'>
					<span class='entry-date'>$date</span></a>
					<span class='meta-sep'> by </span>
					<span class='author vcard'>
					<a class='url fn n' href='?author=$author' title='View all posts for $nick'>$nick</a>
					</span>
			</div><!-- .entry-meta -->

				<div class='entry-content'>
				<p>$content</p>
				</div><!-- .entry-content -->
	
			<div class='entry-utility'>
			This entry was posted in <a href='?cat=$ctg' title='View all posts in $cat' rel='category'>$cat</a>.
			Bookmark the <a href='?p=$id' title='Permalink to $title' rel='bookmark'>permalink</a>.
			Follow any comments here with the <a href='?feed=rss2&amp;p=$id' title='Comments RSS to $title' rel='alternate' type='application/rss+xml'>RSS feed for this post</a>.
							</div><!-- #entry-utility -->
		</div>";

		my$comments = &GetPostComments($id);
		$post .= $comments;

	return $post;
}

sub RecentPostsList
{
	my $posts;

	my(@pids) = $utils->getGenericArray( {
	table => 'posts', field => 'id', status => 'publish', _type => 'post', order_by => '_date DESC', limit => 5
	} );

	my(@pnames) = $utils->getGenericArray( {
	table => 'posts', field => 'title', status => 'publish', _type => 'post', order_by => '_date DESC', limit => 5
	} );

	$posts .= "<li id='recent-posts-2' class='widget-container widget_recent_entries'>
	<h3 class='widget-title'>Recent Posts</h3>
		<ul>";

	for(my$i=0;$i<@pids;$i++)
	{
		$posts .= "<li><a href='?p=$pids[$i]' title='$pnames[$i]'>$pnames[$i]</a></li>";
	}

	$posts .= "</ul>
			</li>";

	return $posts;
}

sub GetPage
{
	shift if ref($_[0]);
	my$id = shift;
	my($title,$content) = $utils->getGenericArray({ table => 'posts', field => 'title,content', id => $id, status => 'publish', _type => 'page' });

		my $post = "<div id='post-$id' class='post-$id post type-post hentry'>
		<h2 class='entry-title'>$title</h2>

				<div class='entry-content'>
				<p>$content</p>
				</div><!-- .entry-content -->
	
		</div>";

	return $post;
}

sub GetPostComments
{
	shift if ref($_[0]);
	my$pid = shift;

	my(@cids) = $utils->getGenericArray( {
	table => 'comments', field => 'id', _type => 'post', post_id => "$pid", order_by => '_date DESC'
	} );


	my$comments = "<div id='comments'>
			<h3 id='comments-title'>". scalar @cids ." Response(s)</h3>
			<ol class='commentlist'>";


	for (my$i=0;$i<@cids;$i++)
	{
		my($author,$content,$dt,$url) = $utils->getGenericArray( {
		table => 'comments', field => 'author,content,_date,author_url', _type => 'post', id => $cids[$i]
		} );

		my($date,$time) = split /\s/, $dt;

			$comments .= "<li class='comment even thread-even depth-$i' id='li-comment-$i'>
		<div id='comment-$i'>
		<div class='comment-author vcard'>
			<img alt='' src='http://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=40' class='avatar avatar-40 photo avatar-default' height='40' width='40' />
			<cite class='fn'>
			<a href='$url' rel='external nofollow' class='url'>$author</a>
			</cite> <span class='says'>says:</span>
		</div>
		<div class='comment-meta commentmetadata'><a href='?p=$pid#comment-$i'>on $date at $time</a></div>

		<div class='comment-body'>
		<p>$content</p>
		</div>";

	}

	$comments .= "</div><!-- #comments -->";

	return $comments;
}

sub RecentCommentsList
{
	my $comms;

	my(@cids) = $utils->getGenericArray( {
	table => 'comments', field => 'id', _type => 'post', order_by => '_date DESC', limit => 5
	} );

		$comms .= "<li id='recent-comments-2' class='widget-container widget_recent_comments'>
			<h3 class='widget-title'>Recent Comments</h3>
				<ul id='recentcomments'>";

	for(my$i=0;$i<@cids;$i++)
	{
		my($pid,$author,$url) = $utils->getGenericArray( {
		table => 'comments', field => 'post_id,author,author_url', _type => 'post', id => $cids[$i]
		} );
		
		my$ptitle = $utils->getGeneric( {
		table => 'posts', field => 'title', _type => 'post', id => $pid
		} );

		$comms .= "<li class='recentcomments'>
			<a href='$url' rel='external nofollow' class='url'>$author</a> on <a href='?p=$pid#comment-$cids[$i]'>$ptitle</a>
				</li>";
	}

	$comms .= "<li class='recentcomments'>
				No Recent Comments
				</li>" unless (@cids);

	$comms .= "</ul>
			</li>";

	return $comms;
}

sub CategoriesList
{
	my $cats;

	my(@cids) = $utils->getGenericArray( {
	table => 'cats', field => 'id', _type => 'post', order_by => 'name DESC'
	} );

	$cats .= "<li id='categories-2' class='widget-container widget_categories'>
	<h3 class='widget-title'>Categories</h3>
		<ul>";


	for(my$i=0;$i<@cids;$i++)
	{
		my$name = $utils->getGeneric( {
		table => 'cats', field => 'name', _type => 'post', id => $cids[$i]
		} );
		
		my$c = $utils->getGeneric( {
		table => 'posts', field => 'count(*)', _type => 'post', guid => $cids[$i]
		} );

		$cats .= "<li class='cat-item cat-item-$i'>
			<a href='?cat=$i' title='View all posts filed under $name'>$name ($c)</a>
		</li>";
	}

	$cats .= "</ul>
			</li>";

	return $cats;
}

sub ArchivesList
{
	my $arc;
	my%months = &GetMonthsHash;

	my(@yyyymm) = $utils->getGenericArray( {
	table => 'posts', field => 'distinct(left(_date, 7))', _type => 'post', status => 'publish' 
	} );


	$arc .= "<li id='archives-2' class='widget-container widget_archive'>
	<h3 class='widget-title'>Archives</h3>
		<ul>";

	for(my$i=0;$i<@yyyymm;$i++)
	{
		my($y,$m) = split /\-/, $yyyymm[$i];

		$arc .= "<li><a href='?m=$y$m' title='$months{$m} $y'>$months{$m} $y</a></li>";
	}

	$arc .= "</ul>
			</li>";

	return $arc;
}


sub PagesList
{
	my $pages;

	my(@pids) = $utils->getGenericArray( {
	table => 'posts', field => 'id', _type => 'page', order_by => 'menu_order DESC'
	} );

	for(my$i=0;$i<@pids;$i++)
	{
		my($ptitle) = $utils->getGeneric( {
		table => 'posts', field => 'title', _type => 'page', id => $pids[$i]
		} );

		$pages .= "<li class='page_item page-item-2' style='z-index:99'>
					<a href='?page_id=$pids[$i]' title='$ptitle'>$ptitle</a>
						</li>";
	}

	return $pages;
}


sub GetMonthsHash
{
	my%months = (
		'01' => 'January',
		'02' => 'February',
		'03' => 'March',
		'04' => 'April',
		'05' => 'May',
		'06' => 'June',
		'07' => 'July',
		'08' => 'August',
		'09' => 'September',
		'10' => 'October',
		'11' => 'November',
		'12' => 'December',
	);

	return %months;

}

1;
